package logic_setting_menu

import (
	v1 "Sailor/app/Sailor-console-server/api/api_setting_menu/v1"
	"Sailor/app/Sailor-console-server/internal/dao"
	"Sailor/app/Sailor-console-server/internal/model/entity"
	"Sailor/app/Sailor-console-server/utility/utility_menu"
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
)

func (s *sSettingMenu) LogicSettingMenuListAll(ctx context.Context, req *v1.SettingMenuListAllReq) (res *v1.SettingMenuListAllRes, err error) {

	var menus []entity.SysMenu
	err = dao.SysMenu.Ctx(ctx).With(entity.SysMenu{}).
		Where(dao.SysMenu.Columns().MenuParentId, "").
		WhereOr(dao.SysMenu.Columns().MenuParentId, nil).
		OrderAsc(dao.SysMenu.Columns().MenuSort).
		Scan(&menus)
	if err != nil {
		g.Log().Error(ctx, err)
		return nil, gerror.New("获取失败")
	}
	newRes := append(v1.SettingMenuListAllRes{}, utility_menu.MenuFormat(menus)...)
	return &newRes, nil
}
